package com.dc2013.followme;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.database.DataSetObserver;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SpinnerAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.ActionBar.LayoutParams;
import com.actionbarsherlock.internal.widget.IcsSpinner;
import com.actionbarsherlock.view.Menu;
import com.android.volley.AuthFailureError;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.Request.Method;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.dc2013.adapter.LikeAdapter;
import com.dc2013.adapter.MainAdapter;
import com.dc2013.followme.RefreshableListView.OnRefreshListener;
import com.dc2013.util.System_Util;
import com.dc2013.util.URLString;

public class ListLike_Activity extends SherlockActivity
{

	private ActionBar actionbar;
	private TextView actionbar_title;
	private LinearLayout actionbar_menu;
	private ImageView menu1,menu2,menu3;
	private String nickName;
	
	private View customActionbar_layout;
	private RefreshableListView likeList;
	private ArrayList<ImageItem> imageItem_arrayList;
	private ImageItem image_item;
	private LikeAdapter likeAdapter;
	private LayoutInflater inflater;
	private LinearLayout actionbarMenu;
	private View view_for_click;
	RequestQueue queue;
	String access_token;

	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_list_like);
		Intent intent = getIntent();
		access_token = intent.getStringExtra("access_token");
		//액션바 설정
		queue = Volley.newRequestQueue(ListLike_Activity.this);
		actionbar = getSupportActionBar();
		inflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		customActionbar_layout = inflater.inflate(R.layout.actionbar_layout,null);
		actionbar_title = (TextView)customActionbar_layout.findViewById(R.id.actionbar_title);
		
		
		actionbarMenu = (LinearLayout)findViewById(R.id.actionbar_menu_like);
		view_for_click = (View)findViewById(R.id.view_for_click_like);
	
	
		actionbar.setCustomView(customActionbar_layout,
				new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT,
						ActionBar.LayoutParams.MATCH_PARENT));
		actionbar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
		
		
		// 리스트뷰 설정부분
		imageItem_arrayList = new ArrayList<ImageItem>();
		
		// 아이템 추가하는 부분 
	//	image_item = new ImageItem();
	// 여기서 아이템 추가해서 url등등 집어너어야됨 
		StringRequest request = new StringRequest(Method.POST, URLString.MAIN
				+ URLString.AUTH, new Response.Listener<String>() {

			@Override
			public void onResponse(String response) {
				// TODO 자동 생성된 메소드 스텁
				try {
					JSONObject obj = new JSONObject(response);
					Log.d("HttpResult", response);
					int message = obj.getInt("Message");
					if (message == 13){
						JSONArray image_items = obj.getJSONArray("user_like_list");
						for (int i=0;i<image_items.length();i++){
							JSONObject image_item_obj  = image_items.getJSONObject(i);
							ImageItem image_item = new ImageItem(image_item_obj.optString("image_url"),
									image_item_obj.optString("contents_id"),
									image_item_obj.optString("theme_name"),
									image_item_obj.optInt("theme_id")
									);
							imageItem_arrayList.add(image_item);
						}
					}

				} catch (JSONException e) {
					// TODO 자동 생성된 catch 블록
					e.printStackTrace();
				}
			}
		}, new Response.ErrorListener() {
			@Override
			public void onErrorResponse(VolleyError arg0) {
				// TODO 자동 생성된 메소드 스텁
			}
		}) {
			@Override
			protected Map<String, String> getParams() throws AuthFailureError {
				Map<String, String> map = new HashMap<String, String>();
				map.put("access_token", access_token);
				return null;
				// TODO 자동 생성된 메소드 스텁
			}
		};
		queue.add(request);
		likeAdapter = new LikeAdapter(this,R.layout.list_type,imageItem_arrayList);
		
		
		likeList = (RefreshableListView)findViewById(R.id.like_list);
		
		
		
	//	likeList.setAdapter(likeAdapter);
	
		
		
		// pull to refresh 리스너 
		likeList.setOnRefreshListener(new OnRefreshListener() 
		{
			
			@Override
			public void onRefresh(RefreshableListView listView) 
			{
				new NewDataTask().execute(); 
				
			}
		});
	
		
		
	}
	
	private OnClickListener actionbarClick = new OnClickListener() 
	{
		
		@Override
		public void onClick(View v) 
		{
			actionbarMenu.setVisibility(View.VISIBLE);
			view_for_click.setVisibility(View.VISIBLE);

		}
	};
	
	private OnClickListener viewClick = new OnClickListener() 
	{
		
		@Override
		public void onClick(View v) 
		{
			actionbarMenu.setVisibility(View.GONE);
			view_for_click.setVisibility(View.GONE);
			
		}
	};
	
	
	// 여기서 받아오는듯 
    private class NewDataTask extends AsyncTask<Void, Void, String> 
    {

        @Override
        protected String doInBackground(Void... params) 
        {
            try 
            {
                Thread.sleep(3000);
            } 
            catch (InterruptedException e) {}
            
            return "A new list item";
        }

        // 이부분에서 서버에서 받아와서 arrayList에 추가하는거 같음 
        @Override
        protected void onPostExecute(String result)
        {
      //     likeitem_arrayList.add(0, result);
            
            // This should be called after refreshing finished
            likeList.completeRefreshing();

            super.onPostExecute(result);
        }
    }
	
	
	

}
